// Author: Bjorn Schobben

#include "Clock.h"
#include <sstream>
#include <iomanip>

using namespace std;

Clock::Clock(ClockType newType): active(false), time(0), initialTime(0), type(newType), lifeTime(0)
{
}

Clock::Clock(int newTime): active(false), time(newTime), initialTime(0), type(ASCENDING), lifeTime(0)
{
}

Clock::~Clock()
{
}

//Start the clock
void Clock::Start(int newTime)
{
	active = true;
	time = newTime;
	initialTime = newTime;

	UpdateDisplay();
}

//Return the type of the clock (either descending or ascending)
const Clock::ClockType Clock::GetType() const
{
	return type;
}

void Clock::UpdateDisplay()
{
}

//Stop the clock
void Clock::Stop()
{
	active = false;

	UpdateDisplay();
}

//Reset the current value of the clock
void Clock::Reset()
{
	time = initialTime;
}

//Increase the time by the inc value
void Clock::Increase(int inc)
{
	time += inc;

	UpdateDisplay();
}

void Clock::Timeout()
{
}

//Add or substract 1 second from the time, depending on the clock-type
void Clock::Tick()
{
	// Increase the lifetime of this clock
	lifeTime++;

	//If the clock-type is ascending, 1 second is added to the current time
	if (ASCENDING == type)
	{
		time++;
	}
	//If the clock-type is descending, 1 second is substracted from the current time
	else if (DESCENDING == type)
	{
		if (time > 0)
		{
			time--;
			//Time can never go under 0
			if (0 == time)
			{
				Timeout();
			}
		}
	}

	UpdateDisplay();
}

//Return the current value of the time
const int Clock::GetTime() const
{
	return time;
}

// Return the life time of this clock since it was created
const int Clock::GetLifeTime() const
{
	return lifeTime;
}

//Convert a integer time value to a string
std::string Clock::TimeToString()
{
	ostringstream s;


	int minutes = time / 60;
	int seconds = time % 60;

	s << setw(2) << setfill('0') << minutes;
	s << ':';
	s << setw(2) << setfill('0') << seconds;
	
	return s.str();
}